package top.honlnk.czuan.service.impl.module.add;

import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.honlnk.czuan.mapper.AddMapper;
import top.honlnk.czuan.pojo.dto.add.AddMorphemeDTO;
import top.honlnk.czuan.pojo.entity.Prefixes;
import top.honlnk.czuan.pojo.entity.Suffixes;
import top.honlnk.czuan.pojo.entity.Roots;

@Slf4j
@Component
public class AddMorphemeSIModule {
    @Autowired
    private AddMapper addMapper;

    public void addRoot(@NotNull AddMorphemeDTO addMorphemeDTO, String userAccount, Integer onlyId) {
        Roots root = Roots.builder()
                .id(onlyId)
                .root(addMorphemeDTO.getMorpheme())
                .explain(addMorphemeDTO.getExplain())
                .fromWordId(addMorphemeDTO.getFormId())
                .createdBy(userAccount)
                .updateBy(userAccount)
                .build();
        addMapper.addRoot(root);
        // 根据 translationIdList 添加 translation_root 关系
        addMorphemeDTO.getTranslationIdList().forEach(translationId -> addMapper.addRootTranslationRelation(onlyId, (Integer) translationId));
    }

    public void addPrefix(@NotNull AddMorphemeDTO addMorphemeDTO, String userAccount, Integer onlyId) {
        Prefixes prefix = Prefixes.builder()
                .id(onlyId)
                .prefix(addMorphemeDTO.getMorpheme())
                .explain(addMorphemeDTO.getExplain())
                .fromWordId(addMorphemeDTO.getFormId())
                .createdBy(userAccount)
                .updateBy(userAccount)
                .build();
        addMapper.addPrefix(prefix);
        // 根据 translationIdList 添加 translation_prefix 关系
        addMorphemeDTO.getTranslationIdList().forEach(translationId -> addMapper.addPrefixTranslationRelation(onlyId, (Integer) translationId));
    }

    public void addSuffix(@NotNull AddMorphemeDTO addMorphemeDTO, String userAccount, Integer onlyId) {
        Suffixes suffix = Suffixes.builder()
                .id(onlyId)
                .suffix(addMorphemeDTO.getMorpheme())
                .explain(addMorphemeDTO.getExplain())
                .fromWordId(addMorphemeDTO.getFormId())
                .createdBy(userAccount)
                .updateBy(userAccount)
                .build();
        addMapper.addSuffix(suffix);
        // 根据 translationIdList 添加 translation_suffix 关系
        addMorphemeDTO.getTranslationIdList().forEach(translationId -> addMapper.addSuffixTranslationRelation(onlyId, (Integer) translationId));
    }
}
